package coml.jiyun.hp.day03weinfo.base;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseRlvAdapter<T> extends RecyclerView.Adapter<BaseRlvAdapter.ViewHolder> {

    private Context context;
    private ArrayList<T> mList;

    public BaseRlvAdapter(Context context, ArrayList<T> mList) {
        this.context = context;
        this.mList = mList;
    }

    //设置适配器和刷新的时候调用
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(getLayout(), parent, false);
        return new ViewHolder(inflate);
    }

    protected abstract int getLayout();

//    加载一条子条目调用一次这个方法
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        T t = mList.get(position);
        bindData(holder,t);

        if (onItemLongClick != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onItemLongClick.ca(view,position);
                    return true;
                }
            });
        }
    }

    protected abstract void bindData(ViewHolder holder, T t);

    //加载更多
    public void addData(List<T> list){
        if (list != null && list.size()>0){
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    //下拉刷新
    public void updateData(ArrayList<T> list){
        if (list != null && list.size() >0){
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        SparseArray<View> mMap = new SparseArray<>();

        public ViewHolder(View itemView) {
            super(itemView);

        }

        public View findView(@IdRes int id){
            View view = mMap.get(id);
            if (view == null){
                view = itemView.findViewById(id);
                mMap.put(id,view);
            }
            return view;
        }
        //有可能有人传过来的id不是TextView的id，这样强转过来就崩了

        public void setText(@IdRes int id,String text){
            try {
            TextView view = (TextView) findView(id);
            view.setText(text);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
        public interface onItemLongClick{
            void ca(View view,int i);
        }
        onItemLongClick onItemLongClick;

    public void setOnItemLongClick(BaseRlvAdapter.onItemLongClick onItemLongClick) {
        this.onItemLongClick = onItemLongClick;
    }
}
